package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.domain.LitemallCart;
import org.linlinjava.litemall.db.domain.LitemallCoupon;
import org.linlinjava.litemall.db.domain.LitemallCouponUser;
import org.linlinjava.litemall.db.domain.RecommandCoupon;
import org.linlinjava.litemall.db.util.CouponConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CouponVerifyService {

    @Autowired
    private LitemallCouponUserService couponUserService;
    @Autowired
    private LitemallCouponService couponService;



    public RecommandCoupon recommandCoupon(Integer userId, BigDecimal price, List<LitemallCart> checkedGoodsList) {
        BigDecimal tmpCouponPrice = new BigDecimal(0.00);
        Integer tmpCouponId = 0;
        Integer tmpUserCouponId = 0;
        int tmpCouponLength = 0;
        List<LitemallCouponUser> couponUserList = couponUserService.queryAll(userId);
        for (LitemallCouponUser couponUser : couponUserList) {
            LitemallCoupon coupon = checkCoupon(userId, couponUser.getCouponId(), tmpUserCouponId, price, checkedGoodsList);
            if (coupon == null) {
                continue;
            }

            tmpCouponLength++;
            if (tmpCouponPrice.compareTo(coupon.getDiscount()) == -1) {
                tmpCouponPrice = coupon.getDiscount();
                tmpCouponId = coupon.getId();
                tmpUserCouponId = couponUser.getId();
            }
        }
        return new RecommandCoupon(tmpCouponId, tmpUserCouponId, tmpCouponPrice, tmpCouponLength);
    }

    /**
     * 检测优惠券是否适合
     *
     * @param userId
     * @param couponId
     * @param checkedGoodsPrice
     * @return
     */
    public LitemallCoupon checkCoupon(Integer userId, Integer couponId, Integer userCouponId, BigDecimal checkedGoodsPrice, List<LitemallCart> goods) {
        LitemallCoupon coupon = couponService.findById(couponId);
        if (coupon == null) {
            return null;
        }

        LitemallCouponUser couponUser = couponUserService.findById(userCouponId);
        if (couponUser == null) {
            couponUser = couponUserService.queryOne(userId, couponId);
        } else if (!couponId.equals(couponUser.getCouponId())) {
            return null;
        }

        if (couponUser == null) {
            return null;
        }

        // 检查是否超期
        Short timeType = coupon.getTimeType();
        Short days = coupon.getDays();
        LocalDateTime now = LocalDateTime.now();
        if (timeType.equals(CouponConstant.TIME_TYPE_TIME)) {
            if (now.isBefore(coupon.getStartTime()) || now.isAfter(coupon.getEndTime())) {
                return null;
            }
        } else if (timeType.equals(CouponConstant.TIME_TYPE_DAYS)) {
            LocalDateTime expired = couponUser.getAddTime().plusDays(days);
            if (now.isAfter(expired)) {
                return null;
            }
        } else {
            return null;
        }


        // 检测商品是否符合
        Short goodType = coupon.getGoodsType();
        if (goodType.equals(CouponConstant.GET_TYPE_CODE)) { //如果指定商品
            BigDecimal giftPrice = getGiftPrice(coupon, goods);
            if (giftPrice == null) {
                return null;
            } else {
                coupon.setDiscount(giftPrice);
            }
        }

        // 检测是否满足最低消费
        if (checkedGoodsPrice.compareTo(coupon.getMin()) == -1) {
            return null;
        }

        // 检测订单状态
        Short status = coupon.getStatus();
        if (!status.equals(CouponConstant.STATUS_NORMAL)) {
            return null;
        }

        return coupon;
    }

    /**
     * 跟据兑换卷计算可省金额
     *
     * @param coupon
     * @param goods
     * @return 可节省金额, 如果无法使用则返回null
     */
    private BigDecimal getGiftPrice(LitemallCoupon coupon, List<LitemallCart> goods) {
        Integer[] goodsValue = coupon.getGoodsValue();
        if (goodsValue.length == 0) {
            return null;
        }
        BigDecimal tmpPrice = null;
        // 循环查找可用优惠券
        for (LitemallCart good : goods) {
            Integer goodsId = good.getGoodsId(); // 商品id
            for (Integer id : goodsValue) {
                if (id.equals(goodsId)) {
                    if (tmpPrice == null) {
                        tmpPrice = good.getPrice();
                    } else if (tmpPrice.compareTo(good.getPrice()) == -1) {
                        tmpPrice = good.getPrice();
                    }
                }
            }
        }
        return tmpPrice;
    }

}